package com.fanix5.javamvvm.base;

import android.util.Log;

import androidx.lifecycle.ViewModel;

import com.fanix5.javamvvm.api.ApiService;
import com.fanix5.javamvvm.api.RetrofitUtil;
import com.trello.rxlifecycle3.LifecycleProvider;

import java.lang.ref.WeakReference;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public class BaseViewModel extends ViewModel {



    //管理RxJava，主要针对RxJava异步操作造成的内存泄漏
    private CompositeDisposable mCompositeDisposable;



    protected void addSubscribe(Disposable disposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
    }


    @Override
    protected void onCleared() {
        super.onCleared();
        //ViewModel销毁时会执行，同时取消所有异步任务

        Log.e("TAG", "onCleared: ViewModel销毁时会执行，同时取消所有异步任务" );

        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
        }

    }



    protected ApiService getHttp(){
        return RetrofitUtil.getHttpService();
    }







}
